import { defineStore } from 'pinia'
import { TagsView } from '@/store/types/tagsView'

interface View {
  name?: string
  path: string
  meta: {
    title?: string
    noCache?: boolean
  }
}

const useTagsViewStore = defineStore('tagsView', {
  state: (): TagsView => ({
    visitedViews: [],
    cachedViews: [],
  }),
  actions: {
    addView(view: View) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    addVisitedView(view: View) {
      if (this.visitedViews.some((v: { path: any }) => v.path === view.path)) return
      this.visitedViews.push(
        { ...view, title: view.meta.title || 'no-name' },
      )
    },
    addCachedView(view: View) {
      if (this.cachedViews.includes(view.name)) return
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name)
      }
    },

    delView(view: View) {
      return new Promise((resolve) => {
        this.delVisitedView(view)
        this.delCachedView(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        })
      })
    },
    delVisitedView(view: View) {
      return new Promise((resolve) => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1)
            break
          }
        }
        resolve([...this.visitedViews])
      })
    },
    delCachedView(view: View) {
      return new Promise((resolve) => {
        for (const i of this.cachedViews) {
          if (i === view.name) {
            const index = this.cachedViews.indexOf(i)
            this.cachedViews.splice(index, 1)
            break
          }
        }
        resolve([...this.cachedViews])
      })
    },

    delOthersViews(view: View) {
      return new Promise((resolve) => {
        this.delOthersVisitedViews(view)
        this.delOthersCachedViews(view)
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        })
      })
    },
    delOthersVisitedViews(view: View) {
      return new Promise((resolve) => {
        this.visitedViews = this.visitedViews.filter((v: { meta: { affix: any }; path: any }) => v.meta.affix || v.path === view.path)
        resolve([...this.visitedViews])
      })
    },
    delOthersCachedViews(view: View) {
      return new Promise((resolve) => {
        for (const i of this.cachedViews) {
          if (i === view.name) {
            const index = this.cachedViews.indexOf(i)
            this.cachedViews.splice(index, 1)
            break
          }
        }
        resolve([...this.cachedViews])
      })
    },

    delAllViews(view: View) {
      return new Promise((resolve) => {
        this.delAllVisitedViews()
        this.delAllCachedViews()
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews],
        })
      })
    },
    delAllVisitedViews() {
      return new Promise((resolve) => {
        const affixTags = this.visitedViews.filter((tag: { meta: { affix: any } }) => tag.meta.affix)
        this.visitedViews = affixTags
        resolve([...this.visitedViews])
      })
    },
    delAllCachedViews() {
      return new Promise((resolve) => {
        this.cachedViews = []
        resolve([...this.cachedViews])
      })
    },

    updateVisitedView(view: View) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    },
  },
})

export default useTagsViewStore
